package com.kzj.fun.interfaces.impl;

import com.kzj.fun.container.root.RootPanel;
import com.kzj.fun.interfaces.MenuFunction;
import com.kzj.fun.utils.ImageUtil;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

public abstract class PaintGraphMenuFunction implements MenuFunction {
    protected RootPanel rootPanel;
    protected Point startPoint,endPoint;
    protected BufferedImage tempImage;
    public PaintGraphMenuFunction(RootPanel rootPanel){
        this.rootPanel=rootPanel;
    }
    @Override
    public void mousePressed(MouseEvent e) {
        this.startPoint=e.getPoint();
        BufferedImage image = rootPanel.imageHistory.peek();
        tempImage= ImageUtil.deepCopy(image);
        rootPanel.imageHistory.add(tempImage);
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        this.endPoint=e.getPoint();
        rootPanel.imageHistory.pop();
        BufferedImage copy = ImageUtil.deepCopy(tempImage);
        Graphics g = copy.getGraphics();
        if(g instanceof Graphics2D){
            Graphics2D g2d =(Graphics2D) g;
            g2d.setStroke(new BasicStroke(rootPanel.stroke));
            g2d.setColor(rootPanel.color);
            paint(g2d);
        }
        g.dispose();
        rootPanel.imageHistory.add(copy);
        rootPanel.repaint();
    }
    abstract void paint(Graphics2D g2d);
}
